﻿using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace IceDog.Practice.WinForm.Common
{
    internal class FormListViewTestCore
    {
        private FormListViewTestCore() { }
        private ListView _listView;
        private Label lblPath;
        public FormListViewTestCore(ListView lv, Label lblPath)
        {
            this._listView = lv;
            this.lblPath = lblPath;
            folderCol = new StringCollection();
        }
        private StringCollection folderCol;

        public StringCollection FolderCol
        {
            get
            {
                return folderCol;
            }

            set
            {
                folderCol = value;
            }
        }

        public void PaintListView(string rootPath)
        {
            if (string.IsNullOrEmpty(rootPath))
            {
                return;
            }
            ListViewItem lvi = null;
            ListViewItem.ListViewSubItem lvsi = null;
            DirectoryInfo dir = null;
            DirectoryInfo[] dirList = null;
            FileInfo[] fileList = null;
            try
            {
                dir = new DirectoryInfo(rootPath);
                dirList = dir.GetDirectories();
                fileList = dir.GetFiles();
            }
            catch (Exception e)
            {
                MessageBox.Show(e.Message);
            }
            this._listView.Items.Clear();
            lblPath.Text = rootPath;
            _listView.BeginUpdate();
            foreach (var item in dirList)
            {
                lvi = new ListViewItem()
                {
                    Text = item.Name,
                    ImageIndex = 0,
                    Tag = item.FullName
                };
                lvsi = new ListViewItem.ListViewSubItem()
                {
                    Text = ""
                };
                lvi.SubItems.Add(lvsi);
                lvsi = new ListViewItem.ListViewSubItem()
                {
                    Text = item.LastAccessTime.ToString()
                };
                lvi.SubItems.Add(lvsi);
                _listView.Items.Add(lvi);
            }
            foreach (var item in fileList)
            {
                lvi = new ListViewItem()
                {
                    Text = item.Name,
                    ImageIndex = 1,
                    Tag = item.FullName
                };
                lvsi = new ListViewItem.ListViewSubItem()
                {
                    Text = item.Length.ToString()
                };
                lvi.SubItems.Add(lvsi);
                lvsi = new ListViewItem.ListViewSubItem()
                {
                    Text = item.LastAccessTime.ToString()
                };
                lvi.SubItems.Add(lvsi);
                _listView.Items.Add(lvi);
            }
            _listView.EndUpdate();
        }
        public void SetView(RadioButton radio, View view)
        {
            if (radio.Checked)
            {
                this._listView.View = view;
            }
        }
    }
}
